/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tuvalLib;

import astroLib.APC_Time;
import astroLib.EarthPosition;
import astroLib.MagDeclination;
import astroLib.Settings;
import javax.microedition.lcdui.*;
import astroLib.SunMoonPosition;
import java.util.Calendar;
import lang.LocalizationSupport;
/**
 * @author mgeden
 */
public class SunMoonCanvas extends Canvas  {

     private SunMoonPosition mSunMoonPosition;
     private int width,height;
     private Font fontPI;
     private int fontSize;
     private double magVar;
     private Settings preferences;
     private EarthPosition position;
     private boolean isMilitary;
     private double[] field = new double[6];
      /**
     * constructor
     */
    public SunMoonCanvas() {

       

        try {
	    // Set up this canvas to listen to command events
	   // setCommandListener(this);
	    // Add the Exit command
        // backCommand = new Command(lang.LocalizationSupport.getMessage("BACKCOMMAND2"), Command.BACK, 0);
	  //   addCommand(backCommand);
        } catch(Exception e) {
            e.printStackTrace();
        }

    } 
    
    /**
     * paint
     */
    public void paint(Graphics g) {
    // String timesStrings[]={israk,isfirar,asrHanefiStr,istiva};
     width=getWidth();
     height=getHeight();
     g.setColor(0, 0, 0);
     g.fillRect(0, 0, width, height);

     fontPI = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,fontSize);
     g.setFont(fontPI);
     int fontHeight=fontPI.getHeight();
     int yPos,line=0;
     g.setColor(0, 0, 139 );
       String varName[] = {LocalizationSupport.getMessage("QIBLANGLE"),
       LocalizationSupport.getMessage("QIBLAMAGNORTH"),
       LocalizationSupport.getMessage("QIBLADISTANCE"),
       LocalizationSupport.getMessage("MAGVAR2"),
       LocalizationSupport.getMessage("MOONPHASE"),
       LocalizationSupport.getMessage("MOONAZIMUTH"),
       LocalizationSupport.getMessage("ALTITUDE"),
       LocalizationSupport.getMessage("SUNAZIMUTH"),
       LocalizationSupport.getMessage("ALTITUDE"),
       LocalizationSupport.getMessage("Bx"),
       LocalizationSupport.getMessage("By"),
       LocalizationSupport.getMessage("Bz") };
       String varValue[] ={   APC_Time.formatDoubleValue(mSunMoonPosition.getDestinationHeading().getHeading())+"º",
        APC_Time.formatDoubleValue(mSunMoonPosition.getDestinationHeading().getHeading()-magVar)+"º",
        (int) (mSunMoonPosition.getDestinationHeading().getKiloMetres())+"km",
        APC_Time.formatDoubleValue(magVar)+"º",
        APC_Time.formatDoubleValue(mSunMoonPosition.getMoonPhase()*100)+"%",
        APC_Time.formatDoubleValue(mSunMoonPosition.getMoonPosition().getAzimuth())+"º",
        APC_Time.formatDoubleValue(mSunMoonPosition.getMoonPosition().getElevation())+"º",
         APC_Time.formatDoubleValue(mSunMoonPosition.getSunPosition().getAzimuth())+"º",
        APC_Time.formatDoubleValue(mSunMoonPosition.getSunPosition().getElevation())+"º" };
     //String tisNames[] = {lang.LocalizationSupport.getMessage("QIBLANGLE"), LocalizationSupport.getMessage("ISTIVA"),LocalizationSupport.getMessage("ASRISANI"), LocalizationSupport.getMessage("ISFIRAR")};
     g.setColor(0, 250, 0);
     g.setFont(fontPI);
     ++line;
     for (int i=1;i<=4;i++)
         { yPos=(++line)*fontHeight;
              g.setColor(0, 250, 0);
          // g.setColor(65, 105, 225);
           if (i==2||i==4)  g.setColor(144,238,144);
           g.drawString(varName[i-1],width/7,yPos,Graphics.BASELINE| Graphics.LEFT);
           g.drawString(":",width/2,yPos, Graphics.BASELINE| Graphics.HCENTER);
            g.setColor(0, 250, 0);
           //g.setColor(135, 206, 250);
           if (i==2||i==4)  g.setColor(144,238,144);
           g.drawString(varValue[i-1],3*width/4,yPos, Graphics.BASELINE| Graphics.HCENTER);

        }

  //   String varName[] = {lang.LocalizationSupport.getMessage("QIBLANGLE"),lang.LocalizationSupport.getMessage("QIBLAMAGNORTH"),lang.LocalizationSupport.getMessage("QIBLADISTANCE"),lang.LocalizationSupport.getMessage("MAGVAR2"),lang.LocalizationSupport.getMessage("SUNAZIMUTH"), lang.LocalizationSupport.getMessage("ALTITUDE")};


      yPos=(++line)*fontHeight;
      for (int i=4;i<=6;i++){
        yPos=(++line)*fontHeight;
        //g.setColor(65, 105, 225);
        g.setColor(135, 206, 250);
        if (i==4||i==6) g.setColor(65, 105, 225);//g.setColor(144, 238, 144);
        g.drawString(varName[i] ,width/7,yPos,Graphics.BASELINE| Graphics.LEFT);
        g.drawString(":",width/2,yPos, Graphics.BASELINE| Graphics.HCENTER);
        g.setColor(135, 206, 250);
        if (i==4||i==6)  g.setColor(65, 105, 225);
        g.drawString(varValue[i],4*width/5,yPos, Graphics.BASELINE| Graphics.RIGHT);
      }
       //yPos=(++line)*fontHeight;
      for (int i=7;i<=8;i++){
        yPos=(++line)*fontHeight;
        g.setColor( 255,111,0);
        g.drawString(varName[i] ,width/7,yPos,Graphics.BASELINE| Graphics.LEFT);
        g.drawString(":",width/2,yPos, Graphics.BASELINE| Graphics.HCENTER);
        g.setColor(255, 255, 0);
        g.drawString(varValue[i],4*width/5,yPos, Graphics.BASELINE| Graphics.RIGHT);
      }

    }




    public void setPreferences(Settings preferences) {
        this.preferences = preferences;
 //       this.MJD0h = getMJD0h();
        this.position = new EarthPosition(Settings.locationPrefer.getLatitude(), Settings.locationPrefer.getLongitude());
        magVar=getMagneticDeclination(getMJD0h() );
        mSunMoonPosition=new SunMoonPosition (position);
        this.setFullScreenMode(Settings.optionPref.getIsFullScreen());
        this.isMilitary = Settings.optionPref.getIsMilitary();
        setFontSize(Settings.optionPref.getFontSize());



    }
   private double getMagneticDeclination(double MJD0h) {
        return MagDeclination.SGMagVar(position, MJD0h, Settings.locationPrefer.getAltitude(), field);
    }

    private double getMJD0h() {
        Calendar now = Calendar.getInstance();
        double timeZone;
        if (Settings.locationPrefer.getTimeZoneSrc() != 0) {
            timeZone = Settings.locationPrefer.getTimeZone();
            if (Settings.locationPrefer.getIsDSTOn()) {
                timeZone++;
            }
        } else {
            timeZone = now.getTimeZone().getRawOffset() / (60 * 60 * 1000);
        }

        return APC_Time.Mjd(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1, now.get(Calendar.DAY_OF_MONTH), 0, 0, 0) - timeZone / 24.0;
    }
    private  void setFontSize(int fontSize) {
          switch (fontSize) {
            case 0: this.fontSize=8;break;
            case 1: this.fontSize=0;break;
            case 2: this.fontSize=16;break;
    }
    }


  
        
    
} 